import time,os,datetime,sys,MySQLdb,logging

logging.basicConfig(level=logging.DEBUG,
                format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                datefmt='%Y %m %d  %H:%M:%S',
                # filename=filename,
                filename='/home/logs/svncommit.log',
                filemode='a')

g_DB_CONFIG={
	'host':'127.0.0.1',
	'user':'root',
	'passwd':'',
	'db':''
}
def GetCurPathInfo():
    today_time=datetime.datetime.now()
    today_0_time = datetime.datetime(today_time.year, today_time.month, today_time.day) 
    yestoday_time=datetime.datetime.now()-datetime.timedelta(days=0)
    yestoday_0_time = datetime.datetime(yestoday_time.year, yestoday_time.month, yestoday_time.day) 
    todayUnix = int(time.mktime(today_0_time.timetuple()))
    yesterdayUnix = int(time.mktime(yestoday_0_time.timetuple()))
    logging.info('yestertoday : date %s, unix_time %s' %(yestoday_0_time,yesterdayUnix))
    logging.info('today : date %s, unix_time %s' %(today_0_time,todayUnix))
    CurPath='/home/svnroot'
    ChildrenList = os.listdir(CurPath)
    # logging.info(ChildrenList)
    appidList=[]
    index = 0
    for name in ChildrenList: 
    	index = index + 1
    	if index%500==0:
    		logging.info (index)
        if len(name) != 14:
            continue
        dbName = os.path.join(CurPath,name,'db')
        if not os.path.exists(dbName):
            logging.info('db is not exists, appId is '+name)
            continue
        # logging.info(dbName)
        appFileStats = os.stat(os.path.join(CurPath,name))
        appModifyTime = appFileStats.st_mtime
        fileStats = os.stat(dbName)
        modifyTime = fileStats.st_mtime
        if modifyTime > yesterdayUnix :
            appidList.append(name)
            logging.info ('today create appid submit '+name)
    logging.info (appidList)
    logging.info ('len is :' +str(len(appidList)))
    
    yestoday_timeStr='%d-%02d-%02d' % (yestoday_time.year, yestoday_time.month,yestoday_time.day) 
    # logging.info(yestoday_timeStr)
    values=[]
    svn_ip='10.124.156.225'
    for appid in appidList:
        values.append((appid,yestoday_timeStr,svn_ip))
    # logging.info(values)
#    try:
 #       conn=MySQLdb.connect(host=g_DB_CONFIG['host'],user=g_DB_CONFIG['user'],passwd=g_DB_CONFIG['passwd'],port=3306,db=g_DB_CONFIG['db'])
  #      cur=conn.cursor()
   #     cur.executemany('insert ignore into statistic_svncommit(appId,reportDate,svn_ip) values(%s,%s,%s)',values)
    #    conn.commit()
     #   cur.close()
      #  conn.close()	
  #  except MySQLdb.Error,e:
   #     logging.info( "Mysql Error %d: %s" % (e.args[0], e.args[1]))
    #    raise e
    return
   
if __name__ == '__main__':
    try:
        logging.info('-'*60 +'begin Statistic_Svncommit'+'-'*60)
        GetCurPathInfo()
        logging.info('-'*60+'end Statistic_Svncommit'+'-'*60)
    except:
        info = sys.exc_info()
        for file, lineno, function, text in traceback.extract_tb(info[2]):
            logging.error(text)
    else:
        logging.info('SUCCESS')
